package com.et114.core.utility;

/*
 * Copyright (c) 2005 Allways, LLC. All rights reserved.
 * �������� 2005-11-22
 * ���ù����๹����
 */

import java.util.Map;




/**
 *  通过类名实例化对象
 * @
 * <a href="InstancePool.java.html"><b><i>View Source</i></b></a>
 *
 * @作者 daomingw
 * @版本 1.0
 */
public class InstancePool {
    
   

    /**
     * 通过类名获得对象
     * @param className
     * @return
     */
	public static Object get(String className) {
		return _instance._get(className);
	}
	
	/**
	 * 类名作为KEY 把OBJECT 置入池中 
	 * @param className
	 * @param obj
	 */

	public static void put(String className, Object obj) {
		_instance._put(className, obj);
	}

	private InstancePool() {
		_classPool = new java.util.Hashtable(); 
	}

	private Object _get(String className) {
		className = className.trim();

		Object obj = _classPool.get(className);

		if (obj == null) {
			try {
				obj = Class.forName(className).newInstance();
				_put(className, obj);
			}
			catch (ClassNotFoundException cnofe) {
				cnofe.printStackTrace();
			}
			catch (InstantiationException ie) {
				ie.printStackTrace();
			}
			catch (IllegalAccessException iae) {
				iae.printStackTrace();
			}
		}

		return obj;
	}

	private void _put(String className, Object obj) {
		_classPool.put(className, obj);
	}

	private static InstancePool _instance = new InstancePool();

	private Map _classPool;

}